package com.example.owner.lettery1.ui

import android.support.v7.widget.RecyclerView
import android.util.Log
import com.example.owner.lettery1.adapter.RecordAdapter
import com.example.owner.lettery1.base.BaseRecyclerViewActivity
import com.example.owner.lettery1.bean.LotteryBean
import com.example.owner.lettery1.bean.Record
import com.example.owner.lettery1.other.ChampionPresenter

class ChampionListActivity : BaseRecyclerViewActivity<ChampionPresenter>() {

    private var lotteryBean: LotteryBean? = null
    private var adapter: RecordAdapter? = null
    private var all = ArrayList<Record>()

    override fun onInitView() {
        super.onInitView()
        lotteryBean = intent.getSerializableExtra(ChampionActivity.LOTTERY_KEY) as LotteryBean
        title = lotteryBean!!.lotteryName+"往期开奖记录"
        addLoadMore()
        onRefresh()
    }

    override fun onLoadData() {
        showOnLoading()
        val observable = presenter!!.getChampionList(lotteryBean!!.lotteryNo!!, pageIndex)
        observable.subscribe({ list ->
            if (pageIndex == 1)
                all.clear()
            if (adapter == null) {
                all.addAll(list)
                adapter = RecordAdapter(all)
                recyclerView!!.adapter = adapter
            }else{
                /*val startPosition =  all.size -1*/
                all.addAll(list)
                adapter!!.notifyDataSetChanged()
            }
            hideOnLoading()
        }, { t ->
            Log.e("ERROR",t.message)
            hideOnLoading() })
    }

    override fun onCreatePresenter(): ChampionPresenter = ChampionPresenter()

}